package com.simon.annual.service.impl;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.repositoy.IReportNeeqRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IGovernance;

/**
 * 第九节:公司治理及内部控制
 * @author YXM
 * */
@Service
public class FindGovernanceImpl implements IGovernance{
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportNeeqRepositoy reportNeeqRepositoy;
	@Resource
	private Environment env;

	@Override
	public void analysis(Document doc, ReportNeeq reportNeeq, int companyPtrId) {
		
		Element  els = doc.getElementsMatchingOwnText(env.getProperty("pdf.nine")).last();
		//事项是或否
		Element governance = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance"), els, 0);
		Element governanceT = cmpanyPublicInfo.getNextTag(governance, governance);
		Elements governanceTd = governanceT.select("table tr > *");
		if(governanceTd.size()>2){
			reportNeeq.setHasNewGovernancePolicy(governanceTd.get(3).text());
			reportNeeq.setHasProfessionalCommittee(governanceTd.get(5).text());
			reportNeeq.setHasIndependentDirector(governanceTd.get(7).text());
			reportNeeq.setHasInvestmentHouseDirector(governanceTd.get(9).text());
			reportNeeq.setHasSupervisorDisagreement(governanceTd.get(11).text());
			reportNeeq.setHasProfessionalManager(governanceTd.get(13).text());
			reportNeeq.setHasInternalControlSigFault(governanceTd.get(15).text());
			reportNeeq.setHasPolicyAgainstMajorReportError(governanceTd.get(17).text());
		}
		//公司治理基本状况
		Element governanceReview = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance.review"), governance, -5);
		Element governanceReviewValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.governance.review.end"), governanceReview, "",0);
		reportNeeq.setGovernanceReview(governanceReviewValue.text());
		//公司治理机制是否给所有股东提供合适的保护和平等权利的评估意见
		Element shareholder = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance.review.end"), governanceReview, -10);
		Element shareholderValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.governance.shareholder.end"), shareholder, "",0);
		reportNeeq.setEqualShareholderRightEvaluation(shareholderValue.text());
		//公司重大决策是否履行规定程序的评估意见
		Element procedure = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance.shareholder.end"), shareholder, -10);
		Element procedureValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.governance.procedure.end"), procedure, "",0);
		reportNeeq.setProcedureForMajorDecisionEvaluation(procedureValue.text());
		//公司章程的修改情况
		Element constitutionUpdateDtail = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance.constitution"), procedure, -10);
		if(constitutionUpdateDtail!=null){
			Element constitutionUpdateDtailValue = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.governance.constitution.end"), procedure, "",0);
			reportNeeq.setConstitutionUpdateDtail(constitutionUpdateDtailValue.text());
			procedure=constitutionUpdateDtail;
		}
		//三会召开情况
		Element meeting = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.governance.meet"), procedure, -80);
		Element meetingDtail= buildMeet(cmpanyPublicInfo.getNextTag(meeting, meeting));
		Elements meetingDtailTd = meetingDtail.select("table tr > *");
		reportNeeq.setBoardMemberNo(meetingDtailTd.get(4).text());
		reportNeeq.setBoardMeetingReview(meetingDtailTd.get(5).text());
		reportNeeq.setSupervisorMeetingNo(meetingDtailTd.get(7).text());
		reportNeeq.setHasNewGovernancePolicy(meetingDtailTd.get(8).text());
		reportNeeq.setShareholdersMeetingNo(meetingDtailTd.size()>10?meetingDtailTd.get(10).text():"");
		reportNeeq.setShareholderMeetingReview(meetingDtailTd.size()>11?meetingDtailTd.get(11).text():"");
		//三会的召集、召开、表决程序是否符合法律法规要求的评估意见
		Element meetingProcedure = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.meeting.procedure"), meeting, -40);
		if(meetingProcedure!=null){
			Element meetingProcedureValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.meeting.procedure.end"), meetingProcedure, "",0);
			reportNeeq.setMeetingProcedureLegitReview(meetingProcedureValue.text());
			meeting = meetingProcedure;
		}
		//公司治理改进情况
		Element governanceImprovement = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.meeting.procedure.end"), meeting, -60);
		Element governanceImprovementValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.meeting.governance.end"), governanceImprovement, "",0);
		reportNeeq.setGovernanceImprovementReview(governanceImprovementValue.text());
		//投资者关系管理情况
		Element governancePolicy = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.meeting.governance.end"), governanceImprovement, -20);
		Element governancePolicyValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.meeting.policy.end"), governancePolicy, "",0);
		reportNeeq.setHasNewGovernancePolicy(governancePolicyValue.text());
		//董事会下设专门委员会在本年度内履行职责时所提出的重要意见和建议(选填)
		Element investorRelationship = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.meeting.investor"), governancePolicy, -20);
		if(investorRelationship!=null){
			Element investorRelationshipValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.meeting.policy.end"), investorRelationship, "",0);
			reportNeeq.setInvestorRelationshipMgtReview(investorRelationshipValue.text());
		}
		//监事会就年度内监督事项的意见
		Element supervisorSugg = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.supervisor.sugg"), governancePolicy, -30);
		if(supervisorSugg!=null){
			Element supervisorSuggValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.supervisor.sugg.end"), supervisorSugg, "",0);
			reportNeeq.setSupervisorSugg(supervisorSuggValue.text());
			governancePolicy =supervisorSugg;
		}
		//公司保持独立性、自主经营能力的说明
		Element independence = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.supervisor.independence"), governancePolicy, -20);
		if(independence!=null){
			Element independenceValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.supervisor.independence.end"), independence, "",0);
			reportNeeq.setCompanyIndependenceReview(independenceValue.text());
			governancePolicy=independence;
		}
		//对重大内部管理制度的评价
		Element management = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.supervisor.management"), governancePolicy, -20);
		if(management!=null){
			Element managementValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.supervisor.management.end"), management, "",0);
			reportNeeq.setMajorManagementPolicyReview(managementValue.text());
			governancePolicy=management;
		}
		//年度报告差错责任追究制度相关情况
		Element against = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.nine.supervisor.against"), governancePolicy, -20);
		if(against!=null){
			Element againstValue= cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.nine.supervisor.against.end"), against, "",0);
			reportNeeq.setPolicyAgainstMajorReportErrorReview(againstValue.text());
		}
		//第十节 财务报告
		Element audited = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.ten.audited"), against, -5);
		//一、审计报告
		Element auditedT= cmpanyPublicInfo.getNextTag(audited,audited);
		Elements auditedTd = auditedT.select("table tr > *");
		reportNeeq.setIsAudited(auditedTd.get(1).text());
		reportNeeq.setAuditOpinion(auditedTd.get(3).text());
		reportNeeq.setAuditReportSn(auditedTd.get(5).text());
		reportNeeq.setAuditFirm(auditedTd.get(7).text());
		reportNeeq.setAuditFirmAddress(auditedTd.get(9).text());
		reportNeeq.setAuditReportDate(auditedTd.get(11).text());
		reportNeeq.setCpaNames(auditedTd.get(13).text());
		reportNeeq.setIsAuditFirmChanged(auditedTd.size()>15?auditedTd.get(15).text():"");
		reportNeeq.setAuditFirmSuccessiveServiceYear(auditedTd.size()>17?auditedTd.get(17).text():"");
		reportNeeq.setAuditReport(auditedT.select("table tr").last().text());
		reportNeeqRepositoy.save(reportNeeq);
	}

	/**
	 * 合并table换行
	 * */
	private Element buildMeet(Element newElement){
		Elements tbodys = newElement.select("tbody");
		//合并分页table
		if(tbodys.size()>0){
			for (int i = 0; i < tbodys.size()-1; i++) {
				Element body = tbodys.get(i+1);
				Elements  element = body.select("tr");
				if(!element.isEmpty()){
					boolean ps = element.first().children().size()==tbodys.get(i).select("tr").last().children().size();
					if(ps&&!body.select("tr").first().children().first().hasText()){
						Elements eTd = element.first().children();
						for (int j = 0; j < eTd.size(); j++) {
							tbodys.get(i).select("tr").last().child(j).appendText(eTd.get(j).text());
						}
						element.first().remove();
						if(!tbodys.get(i+1).hasText()){
							tbodys.remove(i+1);
						}
					}
				}
			}
		}
		return newElement;
	}
	
	
}
